﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace Sripirom.Pattern.UnitTest.Core.UnitTest
{
    public class AbsConfigurationElementCollection<TElement> : ConfigurationElementCollection
        where TElement : AbsConfigurationElement, new()
    {

        public AbsConfigurationElementCollection()
            {

            }

            public override ConfigurationElementCollectionType CollectionType
            {
                get
                {
                    return ConfigurationElementCollectionType.AddRemoveClearMap;
                }
            }

            protected override ConfigurationElement CreateNewElement()
            {
                return new TElement();
            }

            protected override Object GetElementKey(ConfigurationElement element)
            {
                return ((TElement)element).Id;
            }

            public TElement this[int index]
            {
                get
                {
                    return (TElement)BaseGet(index);
                }
                set
                {
                    if (BaseGet(index) != null)
                    {
                        BaseRemoveAt(index);
                    }
                    BaseAdd(index, value);
                }
            }

            new public TElement this[string Name]
            {
                get
                {
                    return (TElement)BaseGet(Name);
                }
            }

            public int IndexOf(TElement Schema)
            {
                return BaseIndexOf(Schema);
            }

            public void Add(TElement Schema)
            {
                BaseAdd(Schema);
            }
            protected override void BaseAdd(ConfigurationElement element)
            {
                BaseAdd(element, false);
            }

            public void Remove(TElement Schema)
            {
                if (BaseIndexOf(Schema) >= 0)
                    BaseRemove(Schema.Id);
            }

            public void RemoveAt(int index)
            {
                BaseRemoveAt(index);
            }

            public void Remove(string name)
            {
                BaseRemove(name);
            }

            public void Clear()
            {
                BaseClear();
            }
        }

    
}
